package com.Factory;

import com.DTO.MessageDTO;
import com.entity.Message;
import com.entity.User;
import com.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MessageFactory {
    @Autowired
    private UserMapper userMapper;

    public Message newMessage(MessageDTO messageDTO, String targetType) {
        Message message = new Message();
        BeanUtils.copyProperties(messageDTO, message);
        message.setSendTime(new Date());
        message.setTargetType(targetType);
        User user = this.userMapper.selectById(messageDTO.getSendUserId());
        message.setSendUserName(user.getName());
        message.setSendUserHeadURL(user.getHeadShotUrl());
        return message;
    }
}
